﻿namespace Microsoft.VisualStudio.TextTemplating.CommandLine
{
    using System;
    using System.Globalization;
    using System.IO;

    internal class CommandSpecification
    {
        private CommandAttribute commandAttribute;
        private Type commandType;
        private OptionSpecificationCollection optionSpecs;

        public CommandSpecification(Type type)
        {
            this.commandType = type;
            object[] customAttributes = this.commandType.GetCustomAttributes(typeof(CommandAttribute), false);
            this.commandAttribute = customAttributes[0] as CommandAttribute;
            this.optionSpecs = new OptionSpecificationCollection();
            this.optionSpecs.LoadFromType(this.commandType);
        }

        public void PrintFullUsage(TextWriter writer)
        {
            writer.WriteLine(" {0} - {1}", this.Name, this.BriefDescription);
            writer.WriteLine();
            writer.WriteLine(FormatUtility.FormatStringForWidth(this.GeneralInformation, 2, 0, 80));
            writer.WriteLine(" Usage: {0}", this.BriefUsage);
            writer.WriteLine();
            writer.WriteLine(" Options:");
            writer.WriteLine();
            foreach (OptionSpecification specification in this.OptionSpecifications)
            {
                if (specification.Description != null)
                {
                    string toFormat = string.Format(CultureInfo.CurrentCulture, specification.Description, new object[] { specification.OptionName });
                    writer.WriteLine(FormatUtility.FormatStringForWidth(toFormat, 2, 2, 80));
                }
            }
        }

        public bool AllowNoNameOptions
        {
            get
            {
                return this.commandAttribute.AllowNoNameOptions;
            }
        }

        public string BriefDescription
        {
            get
            {
                return this.commandAttribute.BriefDescription;
            }
        }

        public string BriefUsage
        {
            get
            {
                return this.commandAttribute.BriefUsage;
            }
        }

        public Type CommandType
        {
            get
            {
                return this.commandType;
            }
        }

        public string GeneralInformation
        {
            get
            {
                return this.commandAttribute.GeneralInformation;
            }
        }

        public string Name
        {
            get
            {
                return this.commandAttribute.Name;
            }
        }

        public OptionSpecificationCollection OptionSpecifications
        {
            get
            {
                return this.optionSpecs;
            }
        }
    }
}

